package com.eclink.asset.hardware.service;

import com.eclink.asset.hardware.domain.dto.DeviceDTO;
import com.eclink.asset.hardware.domain.dto.DeviceDetailDTO;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 公司Service
 *
 * @author chenshixian
 */
@Service
public class RfidService {

    @Resource
    private RestTemplate restTemplate;

    private final String urlPrev = "http://localhost:8085/rfid/";

    /**
     * 查询
     *
     * @param uuid UUID
     * @return 结果
     */
    public Integer getStatus(String uuid) {

        try {
            Integer res = restTemplate.getForObject(urlPrev + "getStatus/"+uuid, Integer.class);
            if (res == null) {
                return 0;
            }
            return res;
        } catch(Exception ex) {
            ex.printStackTrace();
            return 0;
        }

    }

    public void inventory(String uuid) {
        restTemplate.put(urlPrev + "inventory/"+uuid, Integer.class);
    }

    public void stopInventory(String uuid) {
        restTemplate.put(urlPrev + "stopInventory/"+uuid, Integer.class);
    }

    /**
     * 详细
     *
     * @param id ID
     * @return 结果
     */
    public DeviceDetailDTO getDetail(Long id) {
        DeviceDetailDTO res = restTemplate.getForObject("http://localhost:3000/device/getDetail/"+id, DeviceDetailDTO.class);
        return res;
    }

    /**
     * 发布
     *
     * @param dto DTO
     */
    public void publish(DeviceDTO dto) {
        restTemplate.put("http://localhost:3000/device/publish", dto);
    }

    /**
     * 关闭
     *
     * @param id ID
     */
    public void shutdown(Long id) {
        restTemplate.put("http://localhost:3000/device/shutdown/"+id, null);
    }

    /**
     * 启动
     *
     * @param id ID
     */
    public void start(Long id) {
        restTemplate.put("http://localhost:3000/device/start/"+id, null);
    }

}
